﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TDT.Helper;
using System.Web.Mvc;
using System.Collections;
using System.Web.UI.WebControls;
using System.Reflection;
using System.ComponentModel;
using System.Net.Mail;
using System.Net;

namespace TDT.Land.Utilities
{
    /// <summary>
    /// Lớp commons : dùng để chứa các hàm dùng chung
    /// </summary>
    public class Mailer
    {
        const string SmtpServer = "smtp.gmail.com";
        const int SmtpPort = 587;
        const string ToAddress = "kimthanhhousecontact@gmail.com";
        const string Password = "kim@thanh";
        const string ToMailCompany = "kimthanhhouse@gmail.com";
        //const string ToMailCompany = "duonggiaduy1990@gmail.com";

        #region Mail Utilities
        public static bool SendMail(string fromMail, string body, string subject, string cusName, string numPhone)
        {
            bool sendSuccess = false;
            var fromAddress = fromMail;
            //if (fromMail.Split('@').Any())
            //{
            //    fromAddress = fromMail.Split('@')[0];
            //}
            
            try
            {

                var sub_body = @"
 <center> 
    <p>=======================================================</p>
    <p><b>=== Mail Liên Hệ từ Website KimThanhHouse ===</b></p> 
    <p><b>Tên:</b> " + cusName + @"</p>   
    <p><b>Mail:</b>" + fromMail + @"</p>
    <p><b>Phone:</b>" + numPhone + @"</p>
    <p>=======================================================</p>
</center>
";

                MailMessage msg = new MailMessage();
                msg.IsBodyHtml = true;
                msg.Body = sub_body + body;
                msg.From = new MailAddress(fromMail, fromAddress);
                msg.To.Add(new MailAddress(ToMailCompany, ToMailCompany));
                //msg.Bcc.Add(new MailAddress("giathanh0612@gmail.com"));
                msg.Subject = subject;
                SmtpClient client = new SmtpClient(SmtpServer, SmtpPort);
                client.Credentials = new NetworkCredential(ToAddress, Password);
                client.EnableSsl = true;
                client.Send(msg);
                sendSuccess = true;
            }
            catch (Exception ex)
            {
                sendSuccess = false;
                throw;
            }
            return sendSuccess;
        }
        #endregion
    }

}
